package com.atguigu.yygh.hosp.Controller.api;

/**
 * @Author: liyangjing
 * @Date: 2022/04/01/22:20
 * @Description:
 */

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.helper.HttpRequestHelper;
import com.atguigu.yygh.hosp.Service.DepartmentService;
import com.atguigu.yygh.hosp.Service.HospitalService;
import com.atguigu.yygh.hosp.Service.HospitalSetService;
import com.atguigu.yygh.hosp.Service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.utils.MD5;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院系统转体过来的签名，签名进行MD5加密
       String hospSign = (String)paramMap.get("sign");

       //根据传递过来医院编码，查询数据库，查询签名
        String hoscode =(String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        if(!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        String logoData = (String)paramMap.get("logoData");
        logoData = logoData.replaceAll("","+");
        paramMap.put("logoData",logoData);
        //调用Service方法
        hospitalService.save(paramMap);
        return  Result.ok();


    }

    @PostMapping("/hospital/show")
    public  Result getHospital(HttpServletRequest request){

        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //获取医院系统转体过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        if(!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);

    }

    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //获取医院系统转体过来的签名，签名进行MD5加密
        String hospSign = (String)paramMap.get("sign");

        //根据传递过来医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        if(!hospSign.equals(signKeyMD5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        departmentService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        //当前页和每天记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
        //TODO 签名验签
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }


    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        //获取医院系统转体过来的签名，签名进行MD5加密
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {


        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //TODO 签名

        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
    //  必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
    //  非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        //TODO 签名

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {

        Map<String, String[]> requestMap = request.getParameterMap();

        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        //TODO 签名校验

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }







}
